
#include <testpackage.h>
#include "package.h"
#include "linked_list.h"

jstatus_t
TESTPACKAGE_API
package_create(struct judge_fs **fs, const char *file_path) {
	struct package *p = NULL;

	jstatus_t st = JSTATUS_SUCCESS;
	p = package_new();
	if (p == NULL)
		return JSTATUS_BAD_ALLOC;
	st = package_assign(p, file_path);
	if (!JSUCCESS(st)) {
		package_delete(p);
		return st;
	}
	*fs = (struct judge_fs *)p;
	return JSTATUS_SUCCESS;
}

jstatus_t
TESTPACKAGE_API
package_destroy(struct judge_fs *fs) {
	package_delete((struct package *)fs);
	return JSTATUS_SUCCESS;
}

jstatus_t
TESTPACKAGE_API
package_has_spj(struct judge_fs *fs, uint32_t *result) {
	*result = ((struct package *)fs)->has_spj;
	return JSTATUS_SUCCESS;
}

jstatus_t
TESTPACKAGE_API
package_get_metadata(
	struct judge_fs *fs, /* in */
	uint32_t *time_limit, /* out, time_limit */
	uint32_t *memory_limit, /* out, memory_limit */
	uint32_t *testcase_count, /* out, testcase_count */
	const char **testcase_input_path, /* out, testcase_input_path */
	const char **testcase_output_path, /* out, testcase_output_path */
	const char **spj_prefix, /* out, spj_prefix */
	uint32_t *spj_key_count, /* out, spj_key_count */
	const char **spj_key /* out, spj_key */) {

	uint32_t i;
	struct package *p = (struct package *)fs;
	struct list_item *item;

	if (time_limit != NULL)
		*time_limit = p->time_limit;
	if (memory_limit != NULL)
		*memory_limit = p->memory_limit;
	if (testcase_count != NULL)
		*testcase_count = p->testcase_count;
	if (testcase_input_path != NULL) {
		item = p->testcase_list.head;
		for (i = 0; i < p->testcase_count; i += 1, item = item->next) {
			testcase_input_path[i] = (const char *)item->key1;
		}
	}
	if (testcase_output_path != NULL) {
		item = p->testcase_list.head;
		for (i = 0; i < p->testcase_count; i += 1, item = item->next) {
			testcase_output_path[i] = (const char *)item->key2;
		}
	}
	if (spj_prefix != NULL)
		*spj_prefix = p->spj_prefix;
	if (spj_key_count != NULL)
		*spj_key_count = p->spj_key_count;
	if (spj_key != NULL) {
		item = p->spj_key_list.head;
		for (i = 0; i < p->spj_key_count; i += 1, item = item->next)
			spj_key[i] = (const char *)item->key1;
	}
	return JSTATUS_SUCCESS;
}
